package com.k4rthik.clipall.network;

import com.k4rthik.clipall.commons.Level;
import com.k4rthik.clipall.commons.Logger;
import com.k4rthik.clipall.commons.PropertyHandler;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by IntelliJ IDEA.
 * User: k4rthik
 * Date: 3/23/12
 * Time: 10:54 PM
 */

public class ShareServer extends Thread
{
    ServerSocket serverSocket = null;

    public ShareServer() throws IOException
    {
        serverSocket = new ServerSocket(new Integer(PropertyHandler.get("DEFAULT_SERVER_PORT")));
    }

    public ShareServer(int portNumber) throws IOException
    {
        serverSocket = new ServerSocket(portNumber);
    }

    @Override
    public void run()
    {
        while(true) {
            try {
                Logger.log("Waiting for client on port: " + serverSocket.getLocalPort(), Level.INFO);
                Socket connectionSocket = serverSocket.accept();
                Logger.log("Connected to client: "+connectionSocket.getRemoteSocketAddress(), Level.INFO);

                //Saving new connection
                Connections.addClientConnection(connectionSocket);
            } catch (Exception e) {
                Logger.log("Connection to client failed", e);
            }
        }
    }
}
